package com.example.test111.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EsConfig {

    @Value("${es.hostname}")
    private String hostname;

    @Value("${es.port}")
    private int port;
    @Bean
    public ElasticsearchClient elasticsearchClient(){
        RestClient client = RestClient.builder(new HttpHost(hostname, port, "http")).build();
        //RestClientTransport 是 Elasticsearch Transport 模块提供的一个实现，用于实现 Elasticsearch 客户端的底层通信。
        ElasticsearchTransport transport = new RestClientTransport(client, new JacksonJsonpMapper());
        //ElasticsearchClient 是你自定义的 Elasticsearch 客户端类，可能包含了一些封装和业务逻辑
        return new ElasticsearchClient(transport);
    }
}
